package com.longxi.tianjintianbao.ui.presenter.oa;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.Cost;
import com.longxi.tianjintianbao.model.OAPersonnel;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.oa.ICostApplyDetailAtView;
import com.longxi.tianjintianbao.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CostApplyDetailAtPresenter extends BasePresenter<ICostApplyDetailAtView> {

    private String typeName = "";
    private String billId = "";

    Cost cost;
    String mPersonnelId = "";

    public CostApplyDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        billId =intent.getStringExtra(AppConst.BILLID);
        String state =intent.getStringExtra(AppConst.STATE);
        if("已处理".equals(state)){
            getView().getLlBottomBtn().setVisibility(View.GONE);
        }
        typeName =intent.getStringExtra(AppConst.TYPENAME);
        mContext.setTitle(typeName);
        mContext.setTvLeft(MyUtils.getString(R.string.back));
    }

    public void getCostApply(){
        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("billid", billId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getCostApply(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<Cost>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<Cost> costResponse) {
                mContext.hideWaitingDialog();
                if(costResponse.getCode() != 0){
                    MyUtils.showToast(costResponse.getMsg());
                    return;
                }
                cost = costResponse.getData().get(0);
                getView().getEtProjectName().setText(cost.getProjectname());
                getView().getEtUse().setText(cost.getUse());
                getView().getTvLoanType().setText(cost.getLoantype());
                getView().getEtLowerAmount().setText(cost.getLoweramount());
                getView().getEtUpperAmount().setText(cost.getUpperamount());
                getView().getEtSupplier().setText(cost.getSupplier());
                getView().getEtAccounts().setText(cost.getAccounts());
                getView().getEtOpeningBank().setText(cost.getOpeningbank());
                getView().getEtExplain().setText(cost.getExplain());
            }
        });
    }

    /**
     * 选择下级审批人
     */
    public void showPersonnel(String state){
        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("billid", "");
            object.put("userid", MyApplication.loginUser.getUserid());
            object.put("auditingid", cost.getAuditingid());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiRetrofit.getInstance().getOAPersonnel(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<OAPersonnel>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<OAPersonnel> oaPersonnelResponse) {
                mContext.hideWaitingDialog();
                if(oaPersonnelResponse.getCode() != 0){
                    MyUtils.showToast(oaPersonnelResponse.getMsg());
                    return;
                }
                List<String> arrayList = new ArrayList<>();
                List<String> arrayIdList = new ArrayList<>();
                arrayList.add("不选择");
                arrayIdList.add("");
                for (OAPersonnel oaPersonnel : oaPersonnelResponse.getData()) {
                    arrayList.add(oaPersonnel.getPersonnelname());
                    arrayIdList.add(oaPersonnel.getPersonnelid());
                }

                String[] items = arrayList.toArray(new String[]{});
                AlertDialog.Builder builder =new AlertDialog.Builder(mContext);
                builder.setTitle("请选择下级审批人");
                builder.setItems(items, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        mPersonnelId = arrayIdList.get(which);
                        saveOAAuditing(state);
                    }
                });
                builder.show();
            }
        });
    }

    public void saveOAAuditing(String state){
        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("billid", billId);
            object.put("userid", MyApplication.loginUser.getUserid());
            object.put("state", state);
            object.put("auditingid", cost.getAuditingid());
            object.put("personnelid", mPersonnelId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiRetrofit.getInstance().saveOAAuditing(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                mContext.hideWaitingDialog();
                if(baseResponse.getCode() != 0){
                    MyUtils.showToast(baseResponse.getMsg());
                    return;
                }
                MyUtils.showToast("提交成功");
                mContext.setResult(mContext.RESULT_OK);
                mContext.finish();
            }
        });
    }

}
